package com.stylefeng.guns.modular.vpn_channel_info.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.DatasourceEnum;
import com.stylefeng.guns.core.mutidatasource.annotion.DataSource;
import com.stylefeng.guns.modular.vpn.dto.ChannelDTO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.VpnChannelInfo;
import com.stylefeng.guns.modular.vpn_channel_info.service.IVpnChannelInfoService;

/**
 * 推广渠道管理控制器
 *
 * @author fengshuonan
 * @Date 2020-06-05 20:17:30
 */
@Controller
@RequestMapping("/vpnChannelInfo")
public class VpnChannelInfoController extends BaseController {

    private String PREFIX = "/vpn_channel_info/vpnChannelInfo/";

    @Autowired
    private IVpnChannelInfoService vpnChannelInfoService;

    /**
     * 跳转到推广渠道管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "vpnChannelInfo.html";
    }

    /**
     * 跳转到添加推广渠道管理
     */
    @RequestMapping("/vpnChannelInfo_add")
    public String vpnChannelInfoAdd() {
        return PREFIX + "vpnChannelInfo_add.html";
    }

    /**
     * 跳转到修改推广渠道管理
     */
    @RequestMapping("/vpnChannelInfo_update/{vpnChannelInfoId}")
    public String vpnChannelInfoUpdate(@PathVariable Integer vpnChannelInfoId, Model model) {
        VpnChannelInfo vpnChannelInfo = vpnChannelInfoService.selectById(vpnChannelInfoId);
        model.addAttribute("item",vpnChannelInfo);
        LogObjectHolder.me().set(vpnChannelInfo);
        return PREFIX + "vpnChannelInfo_edit.html";
    }

    /**
     * 获取推广渠道管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(ChannelDTO channelDTO) {
        return vpnChannelInfoService.list(channelDTO);
    }

    /**
     * 新增推广渠道管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(VpnChannelInfo vpnChannelInfo) {
        vpnChannelInfoService.addChannel(vpnChannelInfo.getName());
        return SUCCESS_TIP;
    }

    /**
     * 删除推广渠道管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public Object delete(@RequestParam Integer vpnChannelInfoId) {
        vpnChannelInfoService.deleteById(vpnChannelInfoId);
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "/updateChannelStatus")
    @ResponseBody
    public Object updateChannelStatus(@RequestParam("channelId") Integer channelId,
                                      @RequestParam("status") Integer status) {
        vpnChannelInfoService.updateChannelStatus(channelId,status);
        return SUCCESS_TIP;
    }

}
